
var axios = require('axios');
var signUtil = require('./sign');
var Promise = require('promise');
const { config } = require('process');
var baseUrl = "https://api.wrcb.com.cn";
var toke = "/api/login";

function Get(url,params, password,username,key,reqHeard){
    console.log("get")
    if(!reqHeard){
        reqHeard = JSON.stringify({});
    }
    let tokenParam = {
        password:password,
        username:username,
        clientId:key.appId,
        clientSecret:key.appSecret
    };
    return new Promise(function (resolve, reject){
        axios({
            method: 'post',
            url:baseUrl+toke,
            data:tokenParam
        }).then(res=>{
            console.log("token:"+JSON.stringify(res));
            let config = {};
            config.method='get';
            config.params = params;
            let header =  signUtil.sign(config, key.appId, key);
            let salt = header.salt;
            let encryptkey = header.encryptkey;
            if(res.data.code=='200'){
                let headers = {"Authorization":"Bearer "+res.data.result.access_token}
                headers = Object.assign(header.header,headers);
                headers = Object.assign(reqHeard,headers);
                axios({
                    method: 'get',
                    url: baseUrl+url,
                    params: header.reqData,
                    headers: headers
                      }).then(res=>{
                        let repson = signUtil.aesDecrypt(res.data.securityRes,encryptkey,salt)
                        resolve(JSON.parse(repson))
                      }).catch(err=>{
                         reject(err)
                      });
            }else{
                 reject(res)
            }
           
        })  
    })
     
}

function Post(url,params, password,username,key,reqHeard){
    console.log("post")
    if(!reqHeard){
        reqHeard = JSON.stringify({});
    }
    let tokenParam = {
        password:password,
        username:username,
        clientId:key.appId,
        clientSecret:key.appSecret
    };
    return new Promise(function (resolve, reject){
        axios({
            method: 'post',
            url:baseUrl+toke,
            data:tokenParam
        }).then(res=>{
            console.log("token:"+JSON.stringify(res));
            let config = {};
            config.method='post';
            config.data = params;
            let header =  signUtil.sign(config, key.appId, key);
            let salt = header.salt;
            let encryptkey = header.encryptkey;
            if(res.data.code=='200'){
                let headers = {"Authorization":"Bearer "+res.data.result.access_token}
                headers = Object.assign(header.header,headers);
                headers = Object.assign(reqHeard,headers);
                axios({
                    method: 'post',
                    url: baseUrl+url,
                    data: header.reqData,
                    headers: headers
                  }).then(res=>{
                      let repson = signUtil.aesDecrypt(res.data.securityRes,encryptkey,salt)
                     resolve(JSON.parse(repson))
                  }).catch(err=>{
                     reject(err)
                  });
            }else{
                  reject(res)
            }
           
        }) 
    });
    
}
module.exports = {
    Get:Get,
    Post:Post,
    aesDecrypt:signUtil.aesDecrypt
}